package com.ibangnuo.manage.service.order;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ibangnuo.framework.domain.Agent;
import com.ibangnuo.framework.domain.CommercialTenant;
import com.ibangnuo.framework.domain.User;
import com.ibangnuo.framework.domain.request.count.MoneyCountRequest;
import com.ibangnuo.framework.domain.Rate;
import com.ibangnuo.framework.model.response.CommonCode;
import com.ibangnuo.framework.model.response.ObjectResult;
import com.ibangnuo.framework.model.response.QueryResponseResult;
import com.ibangnuo.framework.model.response.QueryResult;
import com.ibangnuo.manage.dao.*;
import com.ibangnuo.manage.dao.employee.AgentMapper;
import com.ibangnuo.manage.dao.employee.AgentRepository;
import com.ibangnuo.manage.dao.employee.CommercialMapper;
import com.ibangnuo.manage.dao.employee.CommercialRepository;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author: ly
 * @Date: 2019-12-31 13:58
 */
@Service
public class OrderCountService {

    private static Logger LOGGER = LoggerFactory.getLogger(OrderCountService.class);

    @Autowired
    private AgentRepository agentRepository;
    @Autowired
    private AgentMapper agentMapper;
    @Autowired
    private OrderTotalMapper orderTotalMapper;
    @Autowired
    private CommercialRepository commercialRepository;
    @Autowired
    private CommercialMapper commercialMapper;
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private OrderTotalMonthMapper orderTotalMonthMapper;
    @Autowired
    private RateRepository rateRepository;

    //登录者的交易日报表查询
    public QueryResponseResult orderReports(MoneyCountRequest moneyCountRequest) throws ParseException {

       /* Date end = moneyCountRequest.getEndTime();
        Date strat = moneyCountRequest.getStratTime();*/
        String stratTime1 = moneyCountRequest.getStratTime();
        String endTime1 = moneyCountRequest.getEndTime();

        Map<String, String> map = new HashMap<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String stratTime = null;
        String endTime = null;
        if (stratTime1 != null) {
            Date strat = sdf.parse(stratTime1);
            stratTime = sdf.format(strat);
        }
        if (endTime1 != null) {
            Date end = sdf.parse(endTime1);
            endTime = sdf.format(end);
        }
        map.put("endTime", endTime);
        map.put("startTime", stratTime);
        int payWay = moneyCountRequest.getPayWay();
        map.put("payWay", String.valueOf(payWay));
        Integer type = null;
        String userId = null;
        User user1 = (User) SecurityUtils.getSubject().getPrincipal();
        type = user1.getType();
        userId = user1.getId();
        int page = 0;
        int size = 0;
        if (moneyCountRequest.getPage() <= 0) {
            page = 1;
        }
        if (moneyCountRequest.getSize() <= 0) {
            size = 5;
        }
        QueryResult queryResult = new QueryResult();
        if (type == 1) {//代理商
            Agent byUserId = agentRepository.findByUserId(userId);
            String id = byUserId.getId();
            map.put("agentId", id);
            PageHelper.startPage(page, size);
            List<Map> list = orderTotalMapper.findByAgentId(map);
            PageInfo<Map> pageInfo = new PageInfo(list);
            queryResult.setList(pageInfo.getList());
        } else if (type == 2) {//商户
            CommercialTenant byUserId = commercialMapper.findByUserId(userId);
            String id = byUserId.getId();
            map.put("commercialId", id);
            PageHelper.startPage(page, size);
            List<Map> list = orderTotalMapper.findByCommercialId(map);
            PageInfo<Map> pageInfo = new PageInfo<Map>(list);
            queryResult.setList(pageInfo.getList());
            queryResult.setTotal(pageInfo.getTotal());
        }
        return new QueryResponseResult(CommonCode.SUCCESS, queryResult);
    }

    //登录者的商户日汇总
    public QueryResponseResult orderReportsCommercial(MoneyCountRequest moneyCountRequest) throws ParseException {
       /* Date end = moneyCountRequest.getEndTime();
        Date strat = moneyCountRequest.getStratTime();*/
        Map map = new HashMap<>();
        String endTime1 = moneyCountRequest.getEndTime();
        String stratTime1 = moneyCountRequest.getStratTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String endTime = null;
        String stratTime = null;
        if (endTime1 != null) {
            Date end = sdf.parse(endTime1);
            endTime = sdf.format(end);
        }
        if (stratTime1 != null) {
            Date strat = sdf.parse(stratTime1);
            stratTime = sdf.format(strat);
        }
        int payWay = moneyCountRequest.getPayWay();
        map.put("startTime", stratTime);
        map.put("endTime", endTime);
        map.put("payWay", String.valueOf(payWay));
        Integer type = null;
        String userId = null;
        User user1 = (User) SecurityUtils.getSubject().getPrincipal();
        if (user1 != null) {
            type = user1.getType();
            userId = user1.getId();
        } else {
            userId = moneyCountRequest.getId();//只是测试数据
            if (userId != null) {
                Optional<User> byId = userRepository.findById(userId);
                if (byId.isPresent()) {
                    type = byId.get().getType();
                    userId = byId.get().getId();
                }
            }
        }
        Agent byUserId1 = agentRepository.findByUserId(userId);
        int page = 0;
        int size = 0;
        if (moneyCountRequest.getPage() <= 0) {
            page = 1;
        }
        if (moneyCountRequest.getSize() <= 0) {
            size = 5;
        }
        QueryResult queryResult = new QueryResult();
        if (type == 1) {//代理商
            map.put("agentId", byUserId1.getId());
            PageHelper.startPage(page, size);
            List<Map> list1 = orderTotalMapper.findByCId(map);
            PageInfo<Map> pageInfo = new PageInfo(list1);
            List<Map> list = pageInfo.getList();
            for (Map map1 : list) {
                String commercialId = (String) map1.get("commercialId");
                Optional<CommercialTenant> byId = commercialRepository.findById(commercialId);
                if (byId.isPresent()) {
                    String companyName = byId.get().getCompanyName();
                    map1.put("commercialCompanyName", companyName);//商户名字
                    String agentId = byId.get().getAgentId();
                    Optional<Agent> byId1 = agentRepository.findById(agentId);
                    if (byId1.isPresent()) {
                        String companyName1 = byId1.get().getCompanyName();
                        map1.put("agentCompanyName", companyName1);//代理商名字
                    }
                }
            }
            queryResult.setList(list);
        } else if (type == 2) {//商户
            CommercialTenant byUserId = commercialMapper.findByUserId(userId);
            String id = byUserId.getId();
            map.put("commercialId", id);
            PageHelper.startPage(page, size);
            List<Map> list = orderTotalMapper.findByCommercialId(map);
            PageInfo<Map> pageInfo = new PageInfo<Map>(list);
            queryResult.setList(pageInfo.getList());
            queryResult.setTotal(pageInfo.getTotal());
        }
        return new QueryResponseResult(CommonCode.SUCCESS, queryResult);

    }

    //代理商的下级代理商的日汇总
    public QueryResponseResult orderReportsAgent(MoneyCountRequest moneyCountRequest) throws ParseException {
        /*Date end = moneyCountRequest.getEndTime();
        Date strat = moneyCountRequest.getStratTime();*/
        Map map = new HashMap<>();
        String stratTime1 = moneyCountRequest.getStratTime();
        String endTime1 = moneyCountRequest.getEndTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String stratTime = null;
        String endTime = null;
        if (stratTime1 != null) {
            Date strat = sdf.parse(stratTime1);
            stratTime = sdf.format(strat);
        }
        if (endTime1 != null) {
            Date end = sdf.parse(endTime1);
            endTime = sdf.format(end);
        }
        int payWay = moneyCountRequest.getPayWay();
        map.put("startTime", stratTime);
        map.put("endTime", endTime);
        map.put("payWay", String.valueOf(payWay));
        Integer type = null;
        String userId = null;
        User user1 = (User) SecurityUtils.getSubject().getPrincipal();
        if (user1 != null) {
            type = user1.getType();
            userId = user1.getId();
        } else {
            userId = moneyCountRequest.getId();//只是测试数据
            if (userId != null) {
                Optional<User> byId = userRepository.findById(userId);
                if (byId.isPresent()) {
                    type = byId.get().getType();
                    userId = byId.get().getId();
                }
            }
        }
        Agent byUserId1 = agentRepository.findByUserId(userId);
        int page = 0;
        int size = 0;
        if (moneyCountRequest.getPage() <= 0) {
            page = 1;
        }
        if (moneyCountRequest.getSize() <= 0) {
            size = 5;
        }
        QueryResult queryResult = new QueryResult();
        if (type == 1) {//代理商
            map.put("agentId", byUserId1.getId());
            PageHelper.startPage(page, size);
            List<Map> list1 = orderTotalMapper.findSecondByAgent(map);
            PageInfo<Map> pageInfo = new PageInfo(list1);
            List<Map> list = pageInfo.getList();
            for (Map map1 : list) {
                String agentId = (String) map1.get("agentId");
                Optional<Agent> byId = agentRepository.findById(agentId);
                if (byId.isPresent()) {
                    String companyName = byId.get().getCompanyName();
                    map1.put("agentCompanyName", companyName);
                }
            }
            queryResult.setList(list);

            queryResult.setTotal(pageInfo.getTotal());
        }
        return new QueryResponseResult(CommonCode.SUCCESS, queryResult);
    }

    //登录者的月汇总
    public QueryResponseResult orderReportsMonth(MoneyCountRequest moneyCountRequest) throws ParseException {
        /*Date end = moneyCountRequest.getEndTime();
        Date strat = moneyCountRequest.getStratTime();*/
        Map<String, String> map = new HashMap<>();
        String stratTime1 = moneyCountRequest.getStratTime();
        String endTime1 = moneyCountRequest.getEndTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        String endTime = null;
        String stratTime = null;
        if (stratTime1 != null) {
            Date strat = sdf.parse(stratTime1);
            stratTime = sdf.format(strat);
        }
        if (endTime1 != null) {
            Date end = sdf.parse(endTime1);
            endTime = sdf.format(end);
        }
        String nowTime = sdf.format(new Date());
        int payWay = moneyCountRequest.getPayWay();
        map.put("startTime", stratTime);
        map.put("endTime", endTime);
        map.put("payWay", String.valueOf(payWay));
        Integer type = null;
        String userId = null;
        User user1 = (User) SecurityUtils.getSubject().getPrincipal();
        if (user1 != null) {
            type = user1.getType();
            userId = user1.getId();
        } else {
            userId = moneyCountRequest.getId();//只是测试数据
            if (userId != null) {
                Optional<User> byId = userRepository.findById(userId);
                if (byId.isPresent()) {
                    type = byId.get().getType();
                    userId = byId.get().getId();
                }
            }
        }
        int page = 0;
        int size = 0;
        if (moneyCountRequest.getPage() <= 0) {
            page = 1;
        } else {
            page = moneyCountRequest.getPage();
        }
        if (moneyCountRequest.getSize() <= 0) {
            size = 5;
        } else {
            size = moneyCountRequest.getSize();
        }
        QueryResult queryResult = new QueryResult();
        if (type == 1) {//代理商
            Agent byUserId = agentRepository.findByUserId(userId);
            String id = byUserId.getId();
            map.put("agentId", id);
            PageHelper.startPage(page, size);
            List<Map> list = orderTotalMonthMapper.findMonthByAgentId(map);//不带本月的汇总
            if (nowTime.equals(endTime)) {//当月的月汇总
                map.put("createTime", nowTime + "-%");
                Map map1 = orderTotalMapper.findMonthByAgentId(map);
                map1.put("createTime", nowTime);
                list.add(map1);
            }
            PageInfo<Map> pageInfo = new PageInfo(list);

            queryResult.setList(pageInfo.getList());
            queryResult.setTotal(pageInfo.getTotal());
        } else if (type == 2) {//商户
            CommercialTenant byUserId = commercialMapper.findByUserId(userId);
            String id = byUserId.getId();
            map.put("commercialId", id);
            PageHelper.startPage(page, size);
            List<Map> list = orderTotalMonthMapper.findMonthByCommercialId(map);//不带本月
            if (nowTime.equals(endTime)) {//当月的月汇总
                map.put("createTime", nowTime + "-%");
                Map map1 = orderTotalMapper.findMonthByCommercialId(map);
                map1.put("createTime", nowTime);
                list.add(map1);
            }
            PageInfo<Map> pageInfo = new PageInfo<Map>(list);
            queryResult.setList(pageInfo.getList());
            queryResult.setTotal(pageInfo.getTotal() + 1);
        }
        return new QueryResponseResult(CommonCode.SUCCESS, queryResult);
    }

    //商户的月交易报表
    public QueryResponseResult monthOrderReportsCommercial(MoneyCountRequest moneyCountRequest) throws ParseException {
        /*Date end = moneyCountRequest.getEndTime();
        Date strat = moneyCountRequest.getStratTime();*/
        Map<String, String> map = new HashMap<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        String endTime1 = moneyCountRequest.getEndTime();
        String stratTime1 = moneyCountRequest.getStratTime();
        String endTime = null;
        String stratTime = null;
        if (endTime1 != null) {
            Date end = sdf.parse(endTime1);
            endTime = sdf.format(end);
        }
        if (stratTime1 != null) {
            Date strat = sdf.parse(stratTime1);
            stratTime = sdf.format(strat);
        }
        String nowTime = sdf.format(new Date());

        int payWay = moneyCountRequest.getPayWay();
        map.put("startTime", stratTime);
        map.put("endTime", endTime);
        map.put("payWay", String.valueOf(payWay));
        Integer type = null;
        String userId = null;
        User user1 = (User) SecurityUtils.getSubject().getPrincipal();
        if (user1 != null) {
            type = user1.getType();
            userId = user1.getId();
        } else {
            userId = moneyCountRequest.getId();//只是测试数据
            if (userId != null) {
                Optional<User> byId = userRepository.findById(userId);
                if (byId.isPresent()) {
                    type = byId.get().getType();
                    userId = byId.get().getId();
                }
            }
        }
        int page = 0;
        int size = 0;
        if (moneyCountRequest.getPage() <= 0) {
            page = 1;
        } else {
            page = moneyCountRequest.getPage();
        }
        if (moneyCountRequest.getSize() <= 0) {
            size = 5;
        } else {
            size = moneyCountRequest.getSize();
        }
        QueryResult queryResult = new QueryResult();
        if (type == 1) {//代理商
            Agent byUserId = agentRepository.findByUserId(userId);
            String id = byUserId.getId();

            map.put("agentId", id);
            PageHelper.startPage(page, size);
            List<Map> list = orderTotalMonthMapper.findByCommercialId(map);//不带本月的汇总
            if (nowTime.equals(endTime)) {//当月的月汇总
                List<CommercialTenant> byAgentId = commercialRepository.findByAgentId(id);
                for (CommercialTenant commercialTenant : byAgentId) {
                    String commercialId = commercialTenant.getId();
                    map.put("commercialId", commercialId);
                    map.put("createTime", nowTime + "-%");
                    Map mapNow = orderTotalMapper.findMonthByCommercialId(map);
                    Optional<CommercialTenant> byId = commercialRepository.findById(commercialId);
                    String companyName = null;
                    String companyNameA = null;
                    if (byId.isPresent()) {
                        companyName = byId.get().getCompanyName();
                        String agentId = byId.get().getAgentId();
                        Optional<Agent> byId1 = agentRepository.findById(agentId);
                        if (byId1.isPresent()) {
                            companyNameA = byId1.get().getCompanyName();
                        }
                    }
                    if (mapNow != null) {
                        mapNow.put("createTime", nowTime);
                        mapNow.put("commercialCompanyName", companyName);
                        mapNow.put("agentCompanyName", companyNameA);
                        list.add(mapNow);
                    }
                }
            }
            PageInfo<Map> pageInfo = new PageInfo(list);

            queryResult.setList(pageInfo.getList());
            queryResult.setTotal(pageInfo.getTotal());
        } else if (type == 2) {//商户
            CommercialTenant byUserId = commercialMapper.findByUserId(userId);
            String id = byUserId.getId();
            String companyNameCommercial = byUserId.getCompanyName();
            map.put("commercialId", id);
            PageHelper.startPage(page, size);
            List<Map> list = orderTotalMonthMapper.findMonthByCommercialId(map);//不带本月
            if (nowTime.equals(endTime)) {//当月的月汇总
                map.put("createTime", nowTime + "-%");
                Map mapNow = orderTotalMapper.findMonthByCommercialId(map);
                if (mapNow != null) {
                    mapNow.put("createTime", nowTime);
                    mapNow.put("commercialCompanyName", companyNameCommercial);
                }
                list.add(mapNow);
            }
            PageInfo<Map> pageInfo = new PageInfo<Map>(list);
            queryResult.setList(pageInfo.getList());
            queryResult.setTotal(pageInfo.getTotal() + 1);
        }
        return new QueryResponseResult(CommonCode.SUCCESS, queryResult);

    }

    //代理商的月交易
    public QueryResponseResult monthOrderReportsAgent(MoneyCountRequest moneyCountRequest) throws ParseException {
        /*Date end = moneyCountRequest.getEndTime();
        Date strat = moneyCountRequest.getStratTime();*/
        Map<String, String> map = new HashMap<>();
        String endTime1 = moneyCountRequest.getEndTime();
        String stratTime1 = moneyCountRequest.getStratTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        String endTime = null;
        String stratTime = null;
        if (endTime1 != null) {
            Date end = sdf.parse(endTime1);
            endTime = sdf.format(end);
        }
        if (stratTime1 != null) {
            Date strat = sdf.parse(stratTime1);
            stratTime = sdf.format(strat);
        }
        String nowTime = sdf.format(new Date());
        int payWay = moneyCountRequest.getPayWay();
        map.put("startTime", stratTime);
        map.put("endTime", endTime);
        map.put("payWay", String.valueOf(payWay));
        Integer type = null;
        String userId = null;
        User user1 = (User) SecurityUtils.getSubject().getPrincipal();
        if (user1 != null) {
            type = user1.getType();
            userId = user1.getId();
        } else {
            userId = moneyCountRequest.getId();//只是测试数据
            if (userId != null) {
                Optional<User> byId = userRepository.findById(userId);
                if (byId.isPresent()) {
                    type = byId.get().getType();
                    userId = byId.get().getId();
                }
            }
        }
        int page = 0;
        int size = 0;
        if (moneyCountRequest.getPage() <= 0) {
            page = 1;
        } else {
            page = moneyCountRequest.getPage();
        }
        if (moneyCountRequest.getSize() <= 0) {
            size = 5;
        } else {
            size = moneyCountRequest.getSize();
        }
        QueryResult queryResult = new QueryResult();
        if (type == 1) {//代理商
            Agent byUserId = agentRepository.findByUserId(userId);
            String id = byUserId.getId();
            map.put("agentId", id);
            PageHelper.startPage(page, size);
            List<Map> list = orderTotalMonthMapper.findByAgentId(map);//不带本月的汇总
            for (Map map1 : list) {
                String agentId = (String) map1.get("agentId");
                Optional<Agent> byId = agentRepository.findById(agentId);
                if (byId.isPresent()) {
                    String companyName = byId.get().getCompanyName();
                    map1.put("agentCompanyName", companyName);
                }
            }
            if (nowTime.equals(endTime)) {//本月的月汇总
                List<Agent> byParentId = agentMapper.findByParentId(id);
                for (Agent agent : byParentId) {
                    map.put("createTime", nowTime + "-%");
                    map.put("agentId", agent.getId());
                    Map mapNow = orderTotalMapper.findMonthByAId(map);
                    if (mapNow != null) {
                        mapNow.put("createTime", nowTime);
                        mapNow.put("agentCompanyName", agent.getCompanyName());
                        list.add(mapNow);
                    }
                }
            }
            PageInfo<Map> pageInfo = new PageInfo(list);
            queryResult.setList(pageInfo.getList());
            queryResult.setTotal(pageInfo.getTotal() + 1);
        }
        return new QueryResponseResult(CommonCode.SUCCESS, queryResult);
    }

    //佣金小计
    public QueryResponseResult commissionDetailed() {
        Map map = new HashMap<>();
        Map returnMap = new HashMap<>();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        String nowTime = sdf.format(new Date());//本月
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(new Date());
        calendar.add(calendar.DATE, -1);
        SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd");
        String yestoday = simple.format(calendar.getTime());

        User user1 = (User) SecurityUtils.getSubject().getPrincipal();
        Integer type = user1.getType();
        if (type == 1) {//代理商
            String userId = user1.getId();
            Agent byUserId = agentRepository.findByUserId(userId);
            map.put("createTime", nowTime + "-%");
            map.put("agentId", byUserId.getId());
            Map mapMonthAlipay = orderTotalMapper.findAlipayByAId(map);

            Map mapMonthWx = orderTotalMapper.findWxByAId(map);

            List<Rate> list = rateRepository.findByAcId(byUserId.getId());
            if (list.size() > 0 && list != null) {
                for (Rate rate : list) {
                    int payWay = rate.getPayWay();
                    Double sAcountA = 0.0;
                    Double sAcountW = 0.0;
                    if (payWay == 0) {//支付宝
                        String rateA = rate.getRate();
                        if (rateA==null){
                            returnMap.put("monthAcountAlipay", 0);
                        }
                        if (mapMonthAlipay != null) {
                            String settleAccounts = (String) mapMonthAlipay.get("settleAccounts");
                            sAcountA = Double.parseDouble(settleAccounts) * Double.parseDouble(rateA);
                            if (sAcountA==null){
                                sAcountA=0.0;
                            }
                            returnMap.put("monthAcountAlipay", sAcountA);
                        }else{
                            returnMap.put("monthAcountAlipay", 0);

                        }
                    } else if (type == 1) {//微信
                        String rateWx = rate.getRate();
                        if (rateWx==null){
                            returnMap.put("monthAcountWx", 0);
                        }
                        if (mapMonthWx != null) {
                            String settleAccounts = (String) mapMonthWx.get("settleAccounts");
                            sAcountW = Double.parseDouble(settleAccounts) * Double.parseDouble(rateWx);
                            if (sAcountW==null){
                                sAcountW=0.0;
                            }
                            returnMap.put("monthAcountWx", sAcountW);
                        }else{

                            returnMap.put("monthAcountWx", 0);
                        }
                    }
                    returnMap.put("monthAcount", sAcountA + sAcountW);
                }
            }

            //日佣金小计
            map.put("createTime", yestoday);//昨天的日期
            Map mapDateAlipay = orderTotalMapper.findDateAlipayByAId(map);
            Map mapDateWx = orderTotalMapper.findDateWxByAId(map);
            if (list.size() > 0 && list != null) {
                for (Rate rate : list) {
                    int payWay = rate.getPayWay();
                    Double sAcountA = 0.0;
                    Double sAcountW = 0.0;
                    if (payWay == 0) {//支付宝
                        String rateA = rate.getRate();
                        if (mapDateAlipay != null) {
                            String settleAccounts = (String) mapDateAlipay.get("settleAccounts");
                            sAcountA = Double.parseDouble(settleAccounts) * Double.parseDouble(rateA);
                            if (sAcountA==null){
                                sAcountA=0.0;
                            }
                            returnMap.put("dateAcountAlipay", sAcountA);
                        }
                    } else if (type == 1) {//微信
                        String rateWx = rate.getRate();
                        if (mapDateWx != null) {
                            String settleAccounts = (String) mapDateWx.get("settleAccounts");
                            sAcountW = Double.parseDouble(settleAccounts) * Double.parseDouble(rateWx);
                            if (sAcountW==null){
                                sAcountW=0.0;
                            }
                            returnMap.put("dateAcountWx", sAcountW);
                        }
                    }
                    returnMap.put("dateAcount", sAcountA + sAcountW);
                }
            }
            if (list.size() ==0 || list == null ){

                returnMap.put("monthAcount", 0);
                returnMap.put("monthAcountWx", 0);
                returnMap.put("monthAcountAlipay", 0);
                returnMap.put("dateAcount", 0);
                returnMap.put("dateAcountWx", 0);
                returnMap.put("dateAcountAlipay", 0);
            }
        } else if (type == 2) {//商户佣金明细
            String userId = user1.getId();
            CommercialTenant byUserId = commercialRepository.findByUserId(userId);
            map.put("createTime", nowTime + "-%");
            map.put("commercialId", byUserId.getId());
            Map mapMonthAlipay = orderTotalMapper.findAlipayByCId(map);
            //结算金额
            if (mapMonthAlipay!=null){
                String settleAccounts = (String) mapMonthAlipay.get("settleAccounts");
                returnMap.put("monthSettleAlipay",settleAccounts);
            }else{
                returnMap.put("monthSettleAlipay",0);
            }
            Map mapMonthWx = orderTotalMapper.findWxByCId(map);
            //结算金额
            if (mapMonthWx!=null){
              String  settleAccounts = (String) mapMonthWx.get("settleAccounts");
                returnMap.put("monthSettleWx",settleAccounts);
            }else{
                returnMap.put("monthSettleWx",0);
            }
            List<Rate> list = rateRepository.findByAcId(byUserId.getId());
            if (list.size() > 0 && list != null) {
                for (Rate rate : list) {
                    int payWay = rate.getPayWay();
                    Double sAcountA = 0.0;
                    Double sAcountW = 0.0;
                    if (payWay == 0) {//支付宝
                        String rateA = rate.getRate();
                        if (mapMonthAlipay != null) {
                            String settleAccounts = (String) mapMonthAlipay.get("settleAccounts");
                            sAcountA = Double.parseDouble(settleAccounts) * Double.parseDouble(rateA);
                            if (sAcountA==null){
                                sAcountA=0.0;
                            }
                            returnMap.put("monthAcountAlipay", sAcountA);
                        }
                    } else if (type == 1) {//微信
                        String rateWx = rate.getRate();
                        if (mapMonthWx != null) {
                            String settleAccounts = (String) mapMonthWx.get("settleAccounts");
                            sAcountW = Double.parseDouble(settleAccounts) * Double.parseDouble(rateWx);
                            if (sAcountW==null){
                                sAcountW=0.0;
                            }
                            returnMap.put("monthAcountWx", sAcountW);
                        }
                    }
                    returnMap.put("monthAcount", sAcountA + sAcountW);
                }
            }
            //佣金日小计
            map.put("createTime", yestoday);//昨天的日期
            Map mapAlipay = orderTotalMapper.findDateAlipayByCId(map);
            Map mapWx = orderTotalMapper.findDateWxByCId(map);
            if (list.size() > 0 && list != null) {
                for (Rate rate : list) {
                    int payWay = rate.getPayWay();
                    Double sAcountA = 0.0;
                    Double sAcountW = 0.0;
                    if (payWay == 0) {//支付宝
                        String rateA = rate.getRate();
                        if (mapAlipay != null) {
                            String settleAccounts = (String) mapAlipay.get("settleAccounts");
                            sAcountA = Double.parseDouble(settleAccounts) * Double.parseDouble(rateA);
                            if (sAcountA==null){
                                sAcountA=0.0;
                            }
                            returnMap.put("dateAcountAlipay", sAcountA);
                        }
                    } else if (type == 1) {//微信
                        String rateWx = rate.getRate();
                        if (mapAlipay != null) {
                            String settleAccounts = (String) mapWx.get("settleAccounts");
                            sAcountW = Double.parseDouble(settleAccounts) * Double.parseDouble(rateWx);
                            if (sAcountW==null){
                                sAcountW=0.0;
                            }
                            returnMap.put("dateAcountWx", sAcountW);
                        }
                    }
                    returnMap.put("dateAcount", sAcountA + sAcountW);
                }
            }
        if (list.size() ==0 || list == null ){

            returnMap.put("monthAcount", 0);
            returnMap.put("monthAcountWx", 0);
            returnMap.put("monthAcountAlipay", 0);
            returnMap.put("dateAcount", 0);
            returnMap.put("dateAcountWx", 0);
            returnMap.put("dateAcountAlipay", 0);
        }
    }
        List<Map> list = new ArrayList<>();
        list.add(returnMap);
        QueryResult queryResult = new QueryResult();
        queryResult.setList(list);
        return new QueryResponseResult(CommonCode.SUCCESS, queryResult);
    }

    //佣金明细月账单
    public QueryResponseResult commissionDetailedMonth(MoneyCountRequest moneyCountRequest) throws ParseException {
        Calendar calendar = new GregorianCalendar();
        /*Date end = moneyCountRequest.getEndTime();
        Date strat = moneyCountRequest.getStratTime();*/
        String endTime1 = moneyCountRequest.getEndTime();
        String stratTime1 = moneyCountRequest.getStratTime();
        SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM");
        Map map = new HashMap<>();
        String stratTime = null;
        String endTime = null;
        if (endTime1 != null) {
            Date end = simple.parse(endTime1);
            calendar.setTime(end);
            calendar.add(calendar.DATE, -1);
            String lastMonth = simple.format(calendar.getTime());//上个月
            String format = simple.format(new Date());
            if (format.equals(endTime1)) {
                endTime = lastMonth;
            } else {
                endTime = simple.format(end);
            }
        }
        if (stratTime1 != null) {
            Date strat = simple.parse(stratTime1);
            stratTime = simple.format(strat);
        }
        map.put("startTime", stratTime);
        map.put("endTime", endTime);
        List<Map> list = new ArrayList<>();
        Map returnMap = new HashMap<>();//计算月佣金的值
        User user1 = (User) SecurityUtils.getSubject().getPrincipal();
        Integer type = user1.getType();
        if (type == 1) {//代理商
            String userId = user1.getId();
            Agent byUserId = agentRepository.findByUserId(userId);
            map.put("agentId", byUserId.getId());

            List<Map> mapAlipay = orderTotalMonthMapper.findAlipayByAId(map);//支付宝的月结算
            List<Map> mapWx = orderTotalMonthMapper.findWxByAId(map);//微信的月结算
            List<Rate> byAcId = rateRepository.findByAcId(byUserId.getId());//代理商的支付宝汇率

            if (byAcId.size() > 0 && byAcId != null) {
                for (Rate rate : byAcId) {
                    Double sAcountA = 0.0;
                    Double sAcountW = 0.0;
                    String rateA = rate.getRate();
                    String rateW = rate.getRate();
                    if (mapAlipay.size() > 0 && mapAlipay != null) {
                        for (Map mapA : mapAlipay) {
                            if (mapWx.size() > 0 && mapWx != null) {
                                for (Map mapW : mapWx) {
                                    String createTimeA = (String) mapA.get("createTime");
                                    String createTimeW = (String) mapW.get("createTime");
                                    if (createTimeA.equals(createTimeW)) {
                                        String settleAccountsA = (String) mapA.get("settleAccounts");
                                        sAcountA = Double.parseDouble(settleAccountsA) * Double.parseDouble(rateA);
                                        String settleAccountsW = (String) mapW.get("settleAccounts");
                                        sAcountW = Double.parseDouble(settleAccountsW) * Double.parseDouble(rateW);
                                        if (sAcountA==null){
                                            sAcountA=0.0;
                                        }
                                        if (sAcountW==null){
                                            sAcountW=0.0;
                                        }
                                        returnMap.put("monthAcountAlipay", sAcountA);
                                        returnMap.put("monthAcountWx", sAcountW);
                                        returnMap.put("createTime", createTimeA);
                                        returnMap.put("monthAcount", sAcountA + sAcountW);
                                        list.add(returnMap);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        } else if (type == 2) {//商户佣金明细
            String userId = user1.getId();
            CommercialTenant byUserId = commercialRepository.findByUserId(userId);
            map.put("commercialId", byUserId.getId());

            List<Map> mapAlipay = orderTotalMonthMapper.findAlipayByCId(map);//支付宝的月结算
            List<Map> mapWx = orderTotalMonthMapper.findWxByCId(map);//微信的月结算
            List<Rate> byAcId = rateRepository.findByAcId(byUserId.getId());//商户的支付宝汇率

            if (byAcId.size() > 0 && byAcId != null) {
                for (Rate rate : byAcId) {
                    Double sAcountA = 0.0;
                    Double sAcountW = 0.0;
                    String rateA = rate.getRate();
                    String rateW = rate.getRate();
                    if (mapAlipay.size() > 0 && mapAlipay != null) {
                        for (Map mapA : mapAlipay) {
                            if (mapWx.size() > 0 && mapWx != null) {
                                for (Map mapW : mapWx) {
                                    String createTimeA = (String) mapA.get("createTime");
                                    String createTimeW = (String) mapW.get("createTime");
                                    if (createTimeA.equals(createTimeW)) {
                                        String settleAccountsA = (String) mapA.get("settleAccounts");
                                        sAcountA = Double.parseDouble(settleAccountsA) * Double.parseDouble(rateA);
                                        String settleAccountsW = (String) mapW.get("settleAccounts");
                                        sAcountW = Double.parseDouble(settleAccountsW) * Double.parseDouble(rateW);
                                        returnMap.put("monthAcountAlipay", sAcountA);
                                        returnMap.put("monthAcountWx", sAcountW);
                                        returnMap.put("createTime", createTimeA);
                                        returnMap.put("monthAcount", sAcountA + sAcountW);
                                        list.add(returnMap);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        QueryResult queryResult = new QueryResult();
        queryResult.setList(list);
        return new QueryResponseResult(CommonCode.SUCCESS, queryResult);
    }

    //佣金日表单
    public QueryResponseResult commissionDetailedDate(MoneyCountRequest moneyCountRequest) throws ParseException {

        int page = 0;
        int size = 0;
        if (moneyCountRequest.getSize() <= 0) {
            size = 5;
        } else {
            size = moneyCountRequest.getSize();
        }
        if (moneyCountRequest.getPage() <= 0) {
            page = 1;
        } else {
            page = moneyCountRequest.getPage();
        }
        Calendar calendar = new GregorianCalendar();
        SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd");
//        Date end = moneyCountRequest.getEndTime();
        Map map = new HashMap<>();
        String endTime1 = moneyCountRequest.getEndTime();
        String endTime = null;
        String stratTime = null;
        if (endTime1 != null) {
            Date end = simple.parse(endTime1);
            calendar.setTime(end);
            calendar.add(calendar.DATE, 0);
            endTime = simple.format(calendar.getTime());
            String nowTime = simple.format(new Date());
            if (nowTime.equals(endTime)) {
                calendar.add(calendar.DATE, -1);
                endTime = simple.format(calendar.getTime());
            }
        }
//        Date strat = moneyCountRequest.getStratTime();
        String stratTime1 = moneyCountRequest.getStratTime();
        if (stratTime1 != null) {
            Date strat = simple.parse(stratTime1);
            stratTime = simple.format(strat);
        }
        map.put("startTime", stratTime);
        map.put("endTime", endTime);
        List<Map> list = new ArrayList<>();
        Map returnMap = new HashMap<>();//计算月佣金的值
        User user1 = (User) SecurityUtils.getSubject().getPrincipal();
        Integer type = user1.getType();
        if (type == 1) {//代理商
            String userId = user1.getId();
            Agent byUserId = agentRepository.findByUserId(userId);
            map.put("agentId", byUserId.getId());

            List<Map> mapAlipay = orderTotalMapper.findDateAlipayByAgentId(map);//支付宝的月结算
            List<Map> mapWx = orderTotalMapper.findDateWxByAgentId(map);//微信的月结算
            List<Rate> byAcId = rateRepository.findByAcId(byUserId.getId());//代理商的汇率

            if (byAcId.size() > 0 && byAcId != null) {
                for (Rate rate : byAcId) {
                    Double sAcountA = 0.0;
                    Double sAcountW = 0.0;
                    String rateA = rate.getRate();
                    String rateW = rate.getRate();
                    if (mapAlipay.size() > 0 && mapAlipay != null) {
                        for (Map mapA : mapAlipay) {
                            if (mapWx.size() > 0 && mapWx != null) {
                                for (Map mapW : mapWx) {
                                    String createTimeA = (String) mapA.get("createTime");
                                    String createTimeW = (String) mapW.get("createTime");
                                    if (createTimeA.equals(createTimeW)) {
                                        String settleAccountsA = (String) mapA.get("settleAccount");
                                        sAcountA = Double.parseDouble(settleAccountsA) * Double.parseDouble(rateA);
                                        String settleAccountsW = (String) mapW.get("settleAccount");
                                        sAcountW = Double.parseDouble(settleAccountsW) * Double.parseDouble(rateW);
                                        returnMap.put("dateAcountAlipay", sAcountA);
                                        returnMap.put("dateAcountWx", sAcountW);
                                        returnMap.put("createTime", createTimeA);
                                        returnMap.put("dateAcount", sAcountA + sAcountW);
                                        list.add(returnMap);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        } else if (type == 2) {//商户佣金明细
            String userId = user1.getId();
            CommercialTenant byUserId = commercialRepository.findByUserId(userId);
            map.put("commercialId", byUserId.getId());

            List<Map> mapAlipay = orderTotalMapper.findDateAlipayByCommercialId(map);//支付宝的月结算
            List<Map> mapWx = orderTotalMapper.findDateWxByCommercialId(map);//微信的月结算
            List<Rate> byAcId = rateRepository.findByAcId(byUserId.getId());//商户的汇率

            if (byAcId.size() > 0 && byAcId != null) {
                for (Rate rate : byAcId) {
                    Double sAcountA = 0.0;
                    Double sAcountW = 0.0;
                    String rateA = rate.getRate();
                    String rateW = rate.getRate();
                    if (mapAlipay.size() > 0 && mapAlipay != null) {
                        for (Map mapA : mapAlipay) {
                            if (mapWx.size() > 0 && mapWx != null) {
                                for (Map mapW : mapWx) {
                                    String createTimeA = (String) mapA.get("createTime");
                                    String createTimeW = (String) mapW.get("createTime");
                                    if (createTimeA.equals(createTimeW)) {
                                        String settleAccountsA = (String) mapA.get("settleAccount");
                                        sAcountA = Double.parseDouble(settleAccountsA) * Double.parseDouble(rateA);
                                        String settleAccountsW = (String) mapW.get("settleAccount");
                                        sAcountW = Double.parseDouble(settleAccountsW) * Double.parseDouble(rateW);
                                        returnMap.put("dateAcountAlipay", sAcountA);
                                        returnMap.put("dateAcountWx", sAcountW);
                                        returnMap.put("createTime", createTimeA);
                                        returnMap.put("dateAcount", sAcountA + sAcountW);
                                        list.add(returnMap);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        PageHelper.startPage(page, size);
        PageInfo<Map> paginfo = new PageInfo<>(list);
        QueryResult queryResult = new QueryResult();
        queryResult.setList(paginfo.getList());
        queryResult.setTotal(list.size());
        return new QueryResponseResult(CommonCode.SUCCESS, queryResult);
    }

    /**
     * 订单统计
     * 1.上一月
     * 2.当前月
     * 3.包含下级代理
     * @return
     */
    public ObjectResult orderSummary(){
        return null;
    }
}
